using System;
using System.Data;
using System.Data.SqlClient;

using Sunyi.Project.WebPoint.Common;
using Sunyi.Project.WebPoint.DataAccess;

using Sunyi.Project.WebPoint.DataContract;

namespace Sunyi.Project.WebPoint.DataAccess
{
    public partial class AttachmentDao : IDao<Attachment>
    {
        public Attachment Select(Guid id)
        {
            Attachment attachment = null;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Attachment_Select";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        attachment = CreateEntityByReader(reader);
                    }
                }
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }

            return attachment;
        }

        public void Insert(Attachment attachment)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Attachment_Insert";
            command.Parameters.Add(new SqlParameter("@Id", attachment.Id));
            command.Parameters.Add(new SqlParameter("@AttachmentName", attachment.AttachmentName));
            command.Parameters.Add(new SqlParameter("@DisplayName", attachment.DisplayName));
            command.Parameters.Add(new SqlParameter("@AttachmentSize", attachment.AttachmentSize));
            command.Parameters.Add(new SqlParameter("@CreatorId", attachment.CreatorId));
            command.Parameters.Add(new SqlParameter("@IsActive", attachment.IsActive));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Update(Attachment attachment)
        {
            if (attachment.UpdateTimestamp == DateTime.MinValue) attachment.UpdateTimestamp = DateTime.Now;

            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Attachment_Update";
            command.Parameters.Add(new SqlParameter("@Id", attachment.Id));
            command.Parameters.Add(new SqlParameter("@AttachmentName", attachment.AttachmentName));
            command.Parameters.Add(new SqlParameter("@DisplayName", attachment.DisplayName));
            command.Parameters.Add(new SqlParameter("@AttachmentSize", attachment.AttachmentSize));
            command.Parameters.Add(new SqlParameter("@CreatorId", attachment.CreatorId));
            command.Parameters.Add(new SqlParameter("@IsActive", attachment.IsActive));
            command.Parameters.Add(new SqlParameter("@UpdateTimestamp", DateTime.Now));

            foreach (SqlParameter parameter in command.Parameters)
            {
                if (parameter.Value == null || parameter.Value.ToString() == Guid.Empty.ToString()) parameter.Value = DBNull.Value;
            }

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        public void Delete(Guid id)
        {
            SqlConnection connection = ConnectionProvider.CreateConnection();
            SqlCommand command = connection.CreateCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "procTR_Attachment_Delete";
            command.Parameters.Add(new SqlParameter("@Id", id));

            try
            {
                connection.Open();
                command.ExecuteNonQuery();
            }
            finally
            {
                ConnectionProvider.CloseConnection(connection);
            }
        }

        #region Private Method

        internal static Attachment CreateEntityByReader(IDataReader reader)
        {
            Attachment attachment = new Attachment();
            if (reader["Id"] != DBNull.Value) attachment.Id = (Guid)reader["Id"];
            if (reader["AttachmentName"] != DBNull.Value) attachment.AttachmentName = reader["AttachmentName"] as string;
            if (reader["DisplayName"] != DBNull.Value) attachment.DisplayName = reader["DisplayName"] as string;
            if (reader["AttachmentSize"] != DBNull.Value) attachment.AttachmentSize = (Int32)reader["AttachmentSize"];
            if (reader["CreatorId"] != DBNull.Value) attachment.CreatorId = (Guid)reader["CreatorId"];
            if (reader["IsActive"] != DBNull.Value) attachment.IsActive = (Boolean)reader["IsActive"];
            if (reader["InsertTimestamp"] != DBNull.Value) attachment.InsertTimestamp = (DateTime)reader["InsertTimestamp"];
            if (reader["UpdateTimestamp"] != DBNull.Value) attachment.UpdateTimestamp = (DateTime)reader["UpdateTimestamp"];

            return attachment;
        }

        #endregion
    }
}